#include<iostream>
#include<unistd.h>
#include<pthread.h>
#include<vector>
#include<string>

using namespace std;

const int threadnum = 5;
class Task
{
public:
    Task(int x, int y):_x(x),_y(y)
    {}
    int Excute()
    {
        return _x + _y;
    }
    ~Task()
    {

    }
private:
    int _x;
    int _y;
};
class ThreadData
{
public:
    ThreadData(int x, int y, const string& threadname):_threadname(threadname), _t(x,y)
    {}
string threadname()
{
    return _threadname;
}
int run()
{
    return _t.Excute();
}
private:
    Task _t;
    string _threadname;
};
class Result
{
public:
    Result(int result, const string &threadname):_result(result),_threadname(threadname)
    {}
    ~Result()
    {}
   void Print()
   {
    cout << _threadname << " : " << _result << endl;
   }
private:
    int _result;
    string _threadname;
};
void* handlerTask(void* argc)
{
    ThreadData *td = (ThreadData*)argc;
    string name = td->threadname();
    int result = td->run();
    Result* res = new Result(result, name);
    delete td;
    sleep(2);
    return res;
}
int main()
{
    vector<pthread_t> thread;
    for(int i = 0; i < threadnum; i++)
    {
        char threadname[64];
        snprintf(threadname, sizeof(threadname), "Thread-%d", i + 1);
        ThreadData *td = new ThreadData(10,20,threadname);

        pthread_t tid;
        pthread_create(&tid, nullptr, handlerTask, td);
        thread.push_back(tid);
    }
    vector<Result*> result_set;
    void* ret = nullptr;
    for(auto& tid : thread)
    {
        pthread_join(tid, &ret);
        result_set.push_back((Result*)ret);
    }
    for(auto& res : result_set)
    {
        res->Print();
        delete res;
    }
    return 0;
}